package com.yixin.ystartlibrary.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

/**
* Created by: WDY
* Date: 2017/3/4 16:22
* Content: 网络状态
*/
public class NetUtils {

    /**
     * 网络是否可用
     * @param context
     * @return
     */
    public static boolean isAvailable(Context context) {
        NetworkInfo info = getNetworkInfo(context);
        if(info != null) {
            return info.isAvailable();
        }
        return false;
    }

    /**
     * 是否是wifi连接
     * @param context
     * @return
     */
    public static boolean isWifi(Context context){
        NetworkInfo info = getNetworkInfo(context);
        if(info != null) {
            return info.getType() == ConnectivityManager.TYPE_WIFI;
        }
        return false;
    }

    /**
     * 是否是移动数据连接
     * @param context
     * @return
     */
    public static boolean isMobileData(Context context){
        NetworkInfo info = getNetworkInfo(context);
        if(info != null) {
            return info.getType() == ConnectivityManager.TYPE_MOBILE;
        }
        return false;
    }

    private static NetworkInfo getNetworkInfo(Context context) {
        if(context != null) {
            ConnectivityManager manager =
                    (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo info = manager.getActiveNetworkInfo();
            return  info;
        }
        return null;
    }
}
